<template>
  <div class="unicode-converter-page">
    <div class="converter-container">
      <h1>Unicode中文互转</h1>
      <el-form>
        <el-form-item label="输入文本">
          <el-input
            v-model="inputText"
            type="textarea"
            :rows="8"
            placeholder="请输入要转换的文本"
            class="text-input"
          />
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="escapeEnglishAndNumbers">
            英文数字是否转义
          </el-checkbox>
        </el-form-item>

        <div class="button-group">
          <el-button type="primary" @click="convertToUnicode">
            <el-icon><Edit /></el-icon>
            转换为Unicode
          </el-button>
          <el-button type="success" @click="convertToChinese">
            <el-icon><ChatRound /></el-icon>
            转换为中文
          </el-button>
          <el-button type="info" @click="copyResult">
            <el-icon><DocumentCopy /></el-icon>
            复制结果
          </el-button>
          <el-button @click="showDemo">
            <el-icon><Monitor /></el-icon>
            示例
          </el-button>
          <el-button type="warning" @click="goToHomepage">
            <el-icon><Back /></el-icon>
            返回
          </el-button>
        </div>
      </el-form>

      <div class="notes">
        <h3>帮助说明：</h3>
        <ul>
          <li>Unicode编码可以表示任意字符，包括中文、日文等</li>
          <li>每个字符都用 \u 开头，后跟4位十六进制数字</li>
          <li>例如：你好 = \u4f60\u597d</li>
          <li>可选择是否对英文和数字进行转义</li>
          <li>支持Unicode编码与中文的双向转换</li>
          <li>转换结果可以直接复制使用</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Edit, ChatRound, DocumentCopy, Back, Monitor } from '@element-plus/icons-vue'
import { copyText } from '@/utils/clipboard'

const router = useRouter()
const inputText = ref('')
const escapeEnglishAndNumbers = ref(false)

const convertToUnicode = () => {
  if (!inputText.value.trim()) {
    ElMessage.warning('请输入需要转换的文本')
    return
  }

  inputText.value = inputText.value.split('').map(char => {
    if (!escapeEnglishAndNumbers.value && /[a-zA-Z0-9]/.test(char)) {
      return char
    }
    return '\\u' + char.charCodeAt(0).toString(16).padStart(4, '0')
  }).join('')

  ElMessage.success('转换成功')
}

const convertToChinese = () => {
  if (!inputText.value.trim()) {
    ElMessage.warning('请输入需要转换的文本')
    return
  }

  try {
    inputText.value = inputText.value.replace(/\\u[\dA-F]{4}/gi, match => {
      return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16))
    })
    ElMessage.success('转换成功')
  } catch (error) {
    ElMessage.error('转换失败，请检查输入格式是否正确')
  }
}

const copyResult = () => {
  if (inputText.value) {
    copyText(inputText.value)
  }
}

const showDemo = () => {
  inputText.value = '你好，Hello123！'
  ElMessage.success('示例文本已加载')
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.unicode-converter-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow-y: auto;

  .converter-container {
    max-width: 800px;
    width: 96%;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    padding: 30px;
    box-sizing: border-box;
    margin: 10px auto;

    h1 {
      text-align: center;
      color: #4a5568;
      font-size: 28px;
      margin-bottom: 30px;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    }

    .el-form {
      .el-form-item {
        margin-bottom: 25px;

        :deep(.el-form-item__label) {
          font-weight: 500;
          color: #4a5568;
          padding-bottom: 8px;
        }

        .text-input {
          :deep(.el-textarea__inner) {
            font-family: 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            padding: 12px;
            border-radius: 8px;
            text-align: left;
            background-color: #f8fafc;
            border-color: #dcdfe6;
            
            &:focus {
              border-color: #409EFF;
              box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
            }
          }
        }

        .el-checkbox {
          color: #606266;
        }
      }
    }

    .button-group {
      display: flex;
      gap: 15px;
      flex-wrap: wrap;
      justify-content: center;
      margin: 20px 0;

      .el-button {
        min-width: 120px;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 8px;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }

        .el-icon {
          font-size: 16px;
        }
      }
    }

    .notes {
      margin-top: 40px;
      padding: 20px;
      background-color: #f0f5ff;
      border-radius: 12px;
      color: #606266;
      text-align: left;

      h3 {
        color: #409EFF;
        margin: 0 0 15px 0;
        font-size: 16px;
        text-align: left;
      }

      ul {
        margin: 0;
        padding-left: 0;
        list-style-type: none;
        text-align: left;

        li {
          margin-bottom: 12px;
          line-height: 1.6;
          position: relative;
          padding-left: 18px;
          text-align: left;

          &:before {
            content: "•";
            color: #409EFF;
            font-size: 18px;
            position: absolute;
            left: 0;
            top: -2px;
          }

          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .unicode-converter-page {
    padding: 10px;

    .converter-container {
      padding: 20px;
      width: 100%;

      h1 {
        font-size: 24px;
        margin-bottom: 20px;
      }

      .button-group {
        flex-direction: column;
        gap: 10px;

        .el-button {
          width: 100%;
          margin: 0;
        }
      }

      .notes {
        padding: 15px;
        margin-top: 30px;

        h3 {
          font-size: 15px;
        }

        ul li {
          font-size: 13px;
          margin-bottom: 8px;
        }
      }
    }
  }
}
</style>
